<?php

namespace App\Repository\GetWay;

use App\Models\GetWay\WayModel as model;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\GetWay\WayParamsRepository;
// use App\Repository\Order\OrderInfoRepository;

class WayRepository extends  BaseRepository
{
  public $model;
  public function __construct()
  {
    $this->model = new model();
    // $this->modelInfo = new OrderInfoRepository();
    parent::__construct();
  }
  public function index($where){
    $cron = $where;
    $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    // $cron['where'] = $where;
    // var_dump($cron,111111111);
    $data = $this->search($this->model, $cron);
    return $data;
  }
  //添加 
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'name',
      'channel_code',
      'channel'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'name' => '通道名称不能为空',
      'channel_code' => '上游商品id不能为空',
      'channel' => '触点编码',
    ];
    if(empty($data['way']) || $data['way'] != 'LianTongZop'){
      return $this->returnMsg(100,'目前仅支持联通接口');
    }
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $isTrue = $this->model->where(['name'=>$data['name']])->first();
    if($isTrue){
      return $this->returnMsg(101,'该渠道名称 已添加');
    }
    $insertDatas['name'] = $data['name'];
    $insertDatas['path'] =  'App\Api\Three\ShopA\LianTongZop';
    $insertDatas['url'] = 'http://cd.10010.com/zop';
    $insertDatas['shop_id'] = $data['user']['shop_id'];
    $insertDatas['parame'] = 'appCode:'.$data['appCode'].';HMACMD5:'.$data['HMACMD5'].';aes:'.$data['aes'].';channel:'.$data['channel'].';product:;referrerCode:;';;
    $res = $this->model->insertGetId($insertDatas);
    if ($res) {
      $models = new WayParamsRepository();
      $data['way_id'] = $res;
      $ress = $models ->add($data);
      if($ress['code'] != 200){
        return $this->returnMsg(1,'添加出现未知错误,请联系开发者同学');
      }
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  public function edit($where,$data){
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }

    //过滤字段
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    // var_dump($field, 222222222222);
    $insertDatas['name'] = $field['name'];
    $insertDatas['path'] =  'App\Api\Three\ShopA\LianTongZop';
    $insertDatas['url'] = 'http://cd.10010.com/zop';
    $insertDatas['parame'] = 'appCode:'.$data['appCode'].';HMACMD5:'.$data['HMACMD5'].';aes:'.$data['aes'].';channel:'.$data['channel'].';product:;referrerCode:;';;
    //先查询是否存在
    $res = $this->model->where($where)->update($insertDatas);
    if ($res) {
      $models = new WayParamsRepository();
      // $data['way_id'] = $res;
      $wheres = [
        'way_id' => $where['id']
      ];
      $ress = $models ->edit($wheres,$data);
      if($ress['code'] != 200){
        return $this->returnMsg(1,'添加出现未知错误,请联系开发者同学');
      }
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
  public function getCount($where)
  {
    return $this->model->where($where)->count();
  }
  public function getAllProduct($where)
  {
    return $this->model->where($where)->get()->toArray();
  }
  // 查询所有的信息
  public function getAllChannel($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->get();
  }
}
